import  React from 'react';
import {connect} from 'react-redux';
import Css from '../../../assets/css/home/goods/index.css';
import action from "../../../store/action";
import {lazyImg, LocalParam} from "../../../assets/js/utils";
import UpRefresh from "../../../assets/js/libs/uprefresh";
import request from "../../../assets/js/utils/request";
class goodsEvaluate extends React.Component{
    constructor(props){
        super(props);
        this.state={
            gid:props.location.search!==''?LocalParam(props.location.search).search:'',
            aEvaArrt:[]
        }
        this.oUpRefresh=null;
        this.curPage=1;
        this.maxPage=0;
        this.offsetBottom=100;
    }
    componentDidMount(){

        this.getGoodsCommodityEva()
    }
    //获取商品评价
    getGoodsCommodityEva(){
        let temGid=this.state.gid.gid;
        this.props.dispatch(action.actGoodsCOmmodityEval.actGoodsCommodityEval({
            temGid,
            success:(val)=>{
                this.maxPage=val.pageinfo.pagenum;
                this.getScrollPage();
                let temAEvarrt=this.props.state.redGoodsCommodityEval.aEvaArrt.data;
                this.setState({aEvaArrt:temAEvarrt},()=>{
                    lazyImg()
                });
            }
        }))
    }
    //获取分页数据.
    getScrollPage(){
        this.oUpRefresh=new UpRefresh({"curPage":this.curPage,"maxPage":this.maxPage,"offsetBottom":this.offsetBottom},curPage=>{
            let url=window.base.config.baseUrl+'/home/reviews/index?gid='+this.state.gid.gid+'&token='+window.base.config.token+'&page='+curPage;
            request(url,'get').then((res)=>{
                if(res.code===200){
                    let temAGoodsSeach=this.state.aEvaArrt;
                    if (res.data.length>0){
                        for (let i in res.data){
                            temAGoodsSeach.push(res.data[i]);
                        }
                    }
                    this.setState({aEvaArrt:temAGoodsSeach},()=>{
                        lazyImg()
                    });
                }
            })
        });
    }
    componentWillUnmount(){
        this.setState=(state,callback)=>{
            return
        }
    }
    render(){
        return(
            <div className={Css['evauate']}>
                <div className={Css['commodityEvaluate']}>
                    <ul>
                        {
                            this.state.aEvaArrt.length>0?
                                this.state.aEvaArrt.map((item,index)=>{
                                    return(
                                        <li key={index} className={'mt40'}>
                                            <div className={Css['commodityMember']}>
                                                <div className={Css['commodityMemberImg']}><img src={require('../../../assets/images/commen/lazyImg.jpg')} data-echo={item.head} alt={item.nickname}/></div>
                                                <div className={Css['commodityMemberNane']}>{item.nickname}</div>
                                            </div>
                                            <div className={Css['commodityDescribe']+' mt20'}>{item.content}</div>
                                            <div className={Css['commodityTime']+' mt10'}>{item.times}</div>
                                        </li>
                                    )
                                })
                                :'没有评价'
                        }
                    </ul>
                </div>
            </div>
        )
    }
}
export default connect((state)=>{
    return{
        state:state
    }
})(goodsEvaluate)